package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.MemberIndexModule;
import cn.com.connext.scrm.repository.master.MemberIndexModuleRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.web.rest.vm.MemberIndexModulePageVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.ZonedDateTime;
import java.util.LinkedList;
import java.util.List;


@Service
@Transactional
public class MemberIndexModuleService {
        @Autowired
        private MemberIndexModuleRepository memberIndexModuleRepository;

        public JsonResult save(MemberIndexModule entity) {
            JsonResult result =new JsonResult();
            if(entity !=null && entity.getId() !=null){
                result.setMessage("修改成功");
            }else{
                entity.setCreateTime(ZonedDateTime.now());
                entity.setIsEnable(1);
                result.setMessage("添加成功");
            }
            memberIndexModuleRepository.saveAndFlush(entity);
            result.setState(0);
            return result;
        }

    public JsonResult deleteModule(MemberIndexModule entity) {
        JsonResult result =new JsonResult();
        if(entity ==null || entity.getId() ==null){
            result.setMessage("参数不能为空");
            result.setState(-1);
            return result;
        }else{
            memberIndexModuleRepository.delete(entity.getId());
            result.setState(0);
            result.setMessage("删除成功");
            return result;
        }
    }

    public Page<MemberIndexModule> findByPage(MemberIndexModulePageVM vm) {
        PageRequest pageRequest = new PageRequest(vm.getPage(), vm.getPageSize(), Sort.Direction.DESC, "createTime");
        Page page = memberIndexModuleRepository.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list = new LinkedList<>();
                Predicate[] pre = new Predicate[list.size()];
                return query.where(list.toArray(pre)).getRestriction();
            }
        }, pageRequest);
        return page;
    }

    public JsonResult findById(Long id) {
        JsonResult result =new JsonResult();
        if(id ==null){
            result.setMessage("参数不能为空");
            result.setState(-1);
            return result;
        }else{
            MemberIndexModule one = memberIndexModuleRepository.findOne(id);
            result.setData(one);
            result.setState(0);
            result.setMessage("查询成功");
            return result;
        }
    }

    public JsonResult updateIsEnable(Long id, Integer isEnable) {
        JsonResult result =new JsonResult();
        if(id ==null || isEnable == null){
            result.setMessage("参数不能为空");
            result.setState(-1);
            return result;
        }else{
            MemberIndexModule one = memberIndexModuleRepository.findOne(id);
            one.setIsEnable(isEnable);
            memberIndexModuleRepository.save(one);
            if(isEnable ==1){
                result.setMessage("启用成功");
            }else if(isEnable ==2){
                result.setMessage("停用成功");
            }
            return result;
        }
    }
}
